<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试记录ID" prop="recordId">
        <el-input
          v-model="queryParams.recordId"
          placeholder="请输入考试记录ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目ID" prop="queId">
        <el-input
          v-model="queryParams.queId"
          placeholder="请输入题目ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目类型" prop="queType">
        <el-select v-model="queryParams.queType" placeholder="请选择题目类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="答案" prop="answer">
        <el-input
          v-model="queryParams.answer"
          placeholder="请输入答案"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="答题类型，0：正确，1：错误" prop="answerType">
        <el-select v-model="queryParams.answerType" placeholder="请选择答题类型，0：正确，1：错误" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="实际得分" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入实际得分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批改状态" prop="markStatus">
        <el-select v-model="queryParams.markStatus" placeholder="请选择批改状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="题目开始时间" prop="answerStartTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.answerStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择题目开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="题目结束时间" prop="answerEndTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.answerEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择题目结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:paperAnswer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam:paperAnswer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam:paperAnswer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['exam:paperAnswer:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperAnswerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="答案ID" align="center" prop="answerId" />
      <el-table-column label="考试记录ID" align="center" prop="recordId" />
      <el-table-column label="题目ID" align="center" prop="queId" />
      <el-table-column label="题目类型" align="center" prop="queType" />
      <el-table-column label="答案" align="center" prop="answer" />
      <el-table-column label="答题类型，0：正确，1：错误" align="center" prop="answerType" />
      <el-table-column label="实际得分" align="center" prop="score" />
      <el-table-column label="批改状态" align="center" prop="markStatus" />
      <el-table-column label="批改内容" align="center" prop="markContent" />
      <el-table-column label="题目开始时间" align="center" prop="answerStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.answerStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="题目结束时间" align="center" prop="answerEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.answerEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:paperAnswer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:paperAnswer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考试答题结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="考试记录ID" prop="recordId">
          <el-input v-model="form.recordId" placeholder="请输入考试记录ID" />
        </el-form-item>
        <el-form-item label="题目ID" prop="queId">
          <el-input v-model="form.queId" placeholder="请输入题目ID" />
        </el-form-item>
        <el-form-item label="题目类型" prop="queType">
          <el-select v-model="form.queType" placeholder="请选择题目类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="答案" prop="answer">
          <el-input v-model="form.answer" placeholder="请输入答案" />
        </el-form-item>
        <el-form-item label="答题类型，0：正确，1：错误" prop="answerType">
          <el-select v-model="form.answerType" placeholder="请选择答题类型，0：正确，1：错误">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="实际得分" prop="score">
          <el-input v-model="form.score" placeholder="请输入实际得分" />
        </el-form-item>
        <el-form-item label="批改状态">
          <el-radio-group v-model="form.markStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="批改内容">
          <editor v-model="form.markContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="题目开始时间" prop="answerStartTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.answerStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择题目开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="题目结束时间" prop="answerEndTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.answerEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择题目结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPaperAnswer, getPaperAnswer, delPaperAnswer, addPaperAnswer, updatePaperAnswer, exportPaperAnswer } from "@/api/exam/paperAnswer";
import Editor from '@/components/Editor';

export default {
  name: "PaperAnswer",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试答题结果表格数据
      paperAnswerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordId: null,
        queId: null,
        queType: null,
        answer: null,
        answerType: null,
        score: null,
        markStatus: null,
        markContent: null,
        answerStartTime: null,
        answerEndTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recordId: [
          { required: true, message: "考试记录ID不能为空", trigger: "blur" }
        ],
        queId: [
          { required: true, message: "题目ID不能为空", trigger: "blur" }
        ],
        queType: [
          { required: true, message: "题目类型不能为空", trigger: "change" }
        ],
        answerType: [
          { required: true, message: "答题类型，0：正确，1：错误不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询考试答题结果列表 */
    getList() {
      this.loading = true;
      listPaperAnswer(this.queryParams).then(response => {
        this.paperAnswerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        answerId: null,
        recordId: null,
        queId: null,
        queType: null,
        answer: null,
        answerType: null,
        score: null,
        markStatus: 0,
        markContent: null,
        answerStartTime: null,
        answerEndTime: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.answerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试答题结果";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const answerId = row.answerId || this.ids
      getPaperAnswer(answerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考试答题结果";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.answerId != null) {
            updatePaperAnswer(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaperAnswer(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const answerIds = row.answerId || this.ids;
      this.$confirm('是否确认删除考试答题结果编号为"' + answerIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPaperAnswer(answerIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有考试答题结果数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPaperAnswer(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
